package cn.donting.jme.edit.ui;

import cn.donting.jme.edit.EditorState;
import cn.donting.jme.edit.JmeEditSceneApp;
import cn.donting.jme.edit.project.Project;
import cn.donting.jme.edit.scene.J3E;
import cn.donting.jme.edit.scene.J3EBuilder;
import cn.donting.jme.edit.ui.scenetree.SceneTreeNodeView;
import com.jme3.system.JmeContext;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * 编辑器主界面的 controller
 */
@Slf4j
public class EditMainApplication {
    @FXML
    public Canvas canvas;
    @FXML
    public AnchorPane canvasPlan;

    /**
     * 用来接受canvas 键盘，鼠标事件的plan
     */
    @FXML
    public Pane inputPlan;
    @FXML
    public AnchorPane topAnchorPane;

    @FXML
    public BorderPane leftBorderPane;
    @FXML
    public BorderPane rightBorderPane;


    private JmeEditSceneApp editSceneApp;

    private Project project;

    public JmeEditSceneApp loadProject(Project project) {
        this.project = project;

        inputPlan.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                inputPlan.requestFocus();
            }
        });


        EditorState.setProject(project);


        //添加fxCanvas 大小变化监听器
        inputPlan.widthProperty().addListener((observable, oldValue, newValue) -> {
            canvas.setWidth(newValue.doubleValue());
            changJmeRenderSize();
        });
        inputPlan.heightProperty().addListener((observable, oldValue, newValue) -> {
            canvas.setHeight(newValue.doubleValue());
            changJmeRenderSize();
        });


        canvas.setWidth(canvasPlan.getWidth());
        canvas.setHeight(canvasPlan.getHeight());


        //创建jme Application
        editSceneApp = new JmeEditSceneApp(canvas);
        try {
            J3E j3E = J3EBuilder.creatNewJ3E(new File(System.getProperty("user.dir") + File.separator + "dev.j3e"));
            editSceneApp.openJ3E(j3E);
        } catch (IOException ex) {
            log.error("j3eEditSavable 加载失败");
            log.error(ex.getMessage(),ex);
        }
        //OffscreenSurface 方式启动
        editSceneApp.start(JmeContext.Type.OffscreenSurface, false);
        EditorState.setJmeEditSceneApp(editSceneApp);
        try {
            //加载场景树view
            loadTreeNode();
            //加载 检查view
            loadInspectorView();
            loadToolBar();
        } catch (Exception ex) {
            log.error("UI FXML 加载失败");
            log.error(ex.getMessage(),ex);
        }
        return editSceneApp;
    }


    private void loadTreeNode() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getClassLoader().getResource("cn/donting/jme/edit/ui/scenetree/SceneTreeNodeView.fxml"));
        Parent parent = fxmlLoader.load();
        SceneTreeNodeView controller = fxmlLoader.getController();
        controller.setRootNode(editSceneApp.getSceneNode());
        leftBorderPane.setCenter(parent);
    }

    private void loadInspectorView() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getClassLoader().getResource("cn/donting/jme/edit/ui/inspector/InspectorView.fxml"));
        Parent parent = fxmlLoader.load();
        rightBorderPane.setCenter(parent);
    }

    private void loadToolBar() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getClassLoader().getResource("cn/donting/jme/edit/tool/ToolBar.fxml"));
        Parent parent = fxmlLoader.load();
        topAnchorPane.getChildren().add(parent);
    }


    /**
     * 改变 jme 渲染大小
     */
    private void changJmeRenderSize() {

        editSceneApp.enqueue(new Runnable() {
            @Override
            public void run() {
                editSceneApp.reshape((int) canvas.getWidth(), (int) canvas.getHeight());
            }
        });
    }


}
